package framework
{
	import mx.collections.ArrayCollection;
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncResponder;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import common.entities.FunctionVO;
	import common.entities.PrivilegeVO;
	
	/**
	 * 基础服务类
	 */
	public class BaseCommand
	{	
		/**远程返回的结果**/
		protected var event:BaseEvent;
		
		/** 远程连接对象 **/
		[Inject]
		public var service:RemoteObject;
		
		/**
		 * invoke async remote method
		 * @param p_method remote method
		 * @param args arguments
		 * @return 
		 * 
		 */		
		protected function call(p_method:String, args:Array):AsyncToken
		{
			if(!p_method)return null;
			service.concurrency = "last";
			//校验权限
			if(AppGlobal.userInfo == null){
				return null;
			}
			var funId:String = "";
			var priviObj:Object = AppGlobal.userInfo["moduleFunctions"];
			var log:Object = AppGlobal.logInfo;
			var functions:ArrayCollection = priviObj[AppGlobal.logInfo.moduleId] as ArrayCollection;
			if(functions){
				for(var i:int = 0;i < functions.length;i++){
					var f:FunctionVO = functions.getItemAt(i) as FunctionVO;
					if(f.identifier == p_method){
						funId = f.id;
						break;
					}
				}
				//根据功能ID找出相应的权限
				if(funId){
					var arrCol:ArrayCollection = AppGlobal.userInfo["privileges"] as ArrayCollection;
					var hasPrivilege:Boolean = false;
					for(var j:int = 0;j < arrCol.length;j++){
						var p:PrivilegeVO = arrCol.getItemAt(j) as PrivilegeVO;
						if(!p)continue;
						if(p.functionId == funId && (!p.moduleId || p.moduleId == AppGlobal.logInfo.moduleId)){
							service.endpoint += "?privilegeId=" + p.id;
							hasPrivilege = true;
							break;
						}
					}
					//没有找到相关权限
					if(!hasPrivilege)
						return null;
				}
			}
			var method:AbstractOperation = service.getOperation(p_method);
			method.arguments = args;
			var token:AsyncToken = method.send();
			if(token){
				AppGlobal.invokeMethods[token.message.messageId] = method;
				token.addResponder(new AsyncResponder(result, error, token));
				return token;
			}
			return null;
		}
		
		/**
		 * 
		 */
		public function execute(p_evt:BaseEvent):AsyncToken
		{
			event = p_evt;
			return call(p_evt.method, p_evt.args);
		}
		
		/**
		 * 
		 * @param p_result
		 */		
		public function result(p_result:ResultEvent, p_token:AsyncToken = null):void
		{
			removeOperation(p_token.message.messageId);
			if(event && event.callback != null){
				event.result = p_result.result;
				event.callback(event);
			}
		}
		
		/**
		 * 
		 */		
		public function error(p_fault:FaultEvent, p_token:AsyncToken = null):void
		{
			removeOperation(p_token.message.messageId);
			//弹出错误信息
			Message.alert(p_fault.fault.rootCause.msgKey);
		}	
		
		/**
		 * 移除操作
		 * @param p_msgId
		 * 
		 */		
		private function removeOperation(p_msgId:String):void
		{
			var method:AbstractOperation = AppGlobal.invokeMethods[p_msgId] as AbstractOperation;
			method.cancel(p_msgId);
			delete AppGlobal.invokeMethods[p_msgId];
		}
	}
}